import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/entity/team/zq_team_detail_entity.dart';
import 'package:ball_lion/server/api/soccer_api.dart';
import 'package:ball_lion/module/team/soccer/zq_team_info_view.dart';
import 'package:ball_lion/module/team/soccer/zq_team_lineup_view.dart';
import 'package:ball_lion/module/team/soccer/zq_team_schedule_view.dart';

class SoccerTeamDetailLogic extends GetxController
    with GetTickerProviderStateMixin {
  ScrollController scrollC = ApmScrollController();
  final _opacity = (-1.0).obs;
  int id = Get.arguments;
  SoccerTeamDetailEntity? data;
  final GlobalKey<ExtendedNestedScrollViewState> scrollKey =
      GlobalKey<ExtendedNestedScrollViewState>();
  final tabs = ['资料', '赛程', '阵容'];
  late final pages = [
    const SoccerTeamInfoView(),
    SoccerTeamScheduleView(teamId: id),
    SoccerTeamLineupView(teamId: id),
  ];
  late TabController tabController =
      TabController(length: tabs.length, vsync: this, initialIndex: 1);

  double get opacity {
    if (_opacity < 0) {
      return 0.0;
    } else if (_opacity > 1) {
      return 1.0;
    } else {
      return _opacity.value;
    }
  }

  @override
  void onInit() {
    scrollC.addListener(() {
      double offset = scrollC.offset;
      _opacity.value = (offset - 120) / 28;
    });
    super.onInit();
  }

  @override
  void onReady() async {
    super.onReady();
    getData();
  }

  Future getData() async {
    final result = await SoccerApi.getSoccerTeamDetail(id);
    if (result != null) {
      data = result;
      update();
    }
  }

  String? getRecentStatus() {
    if (data?.matchResultLast == null) {
      return null;
    }
    return data!.matchResultLast!
        .map((e) {
          if (e.resultStatus == 3) {
            return '胜';
          }
          if (e.resultStatus == 1) {
            return '平';
          }
          if (e.resultStatus == 0) {
            return '负';
          }
        })
        .toList()
        .join('');
  }
}
